﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// ActiveState 的摘要说明
/// </summary>
public class ActiveState
{
    public ActiveState()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    const string constr = "server=60.191.254.162,3433;User ID='ten9158room_web_v2';Password='P@t*5!9Tg';Pooling=true;Max Pool Size=35;database=TenTiange;Connection Reset=FALSE";
    //const string constr = "server=10.191.231.133,3433;User ID='ten9158room_web_v2';Password='P@t*5!9Tg';Pooling=true;Max Pool Size=35;database=TenTiange;Connection Reset=FALSE";
    //const string constr = "server=122.227.23.112,3433;User ID='tenroom';Password='tenroom';Pooling=true;Max Pool Size=35;database=TenRoomTest;Connection Reset=FALSE";
    /// <summary>
    /// 得到排名
    /// </summary>
    /// <returns></returns>      
    public int GetRank()
    {
        int rank = 0;
        DataTable dat = new DataTable();
        SqlParameter[] pars ={ SqlHelper.MakeOutParam("@rank", SqlDbType.Int, 4) };

        SqlHelper.ExecuteNonQuery(constr, CommandType.StoredProcedure, "GetRank", pars);
        rank = Convert.ToInt32(pars[0].Value);
        return rank;
    }

    //设置路线
    public void SetRandWay()
    {
        SqlHelper.ExecuteNonQuery(constr, CommandType.StoredProcedure, "GetRandWay");
    }
    /// <summary>
    /// 得到游戏开始时间
    /// </summary>
    /// <returns></returns>
    public string GetStartDate()
    {


        string return_val = string.Empty;
        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "GetStartDate");

        string[] times = dat.Rows[0][2].ToString().Split(',');
        if (dat.Rows.Count > 0)
        {
            if (Convert.ToInt32(dat.Rows[0][0]) == 1)
            {

                if (DateTime.Now.Date < Convert.ToDateTime(dat.Rows[0][1].ToString().Split(',')[0]))
                {
                    return_val = Convert.ToDateTime(dat.Rows[0][1].ToString().Split(',')[0]).ToLongDateString();
                }
                if (DateTime.Now.Date >= Convert.ToDateTime(dat.Rows[0][1].ToString().Split(',')[0]))
                {
                    if (Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()) < Convert.ToDateTime(times[0] + ":00"))
                    {
                        return_val = DateTime.Now.ToLongDateString();
                    }
                    if (Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()) > Convert.ToDateTime(times[1] + ":00"))
                    {
                        return_val = DateTime.Now.AddDays(1).ToLongDateString();
                    }
                }
                if (DateTime.Now.Date == Convert.ToDateTime(dat.Rows[0][1].ToString().Split(',')[1]))
                {
                    return_val = DateTime.Now.ToLongDateString();
                }
            }
            if (Convert.ToInt32(dat.Rows[0][0]) == 2)
            {
                string[] strarr = dat.Rows[0][1].ToString().Split(',');
                int len = strarr.Length;
                int week = 0;
                for (int k = 0; k < len; k++)
                {

                    if ((int)DateTime.Now.DayOfWeek == Convert.ToInt32(strarr[k]))
                    {
                        if (Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()) < Convert.ToDateTime(times[0] + ":00"))
                        {
                            return_val = DateTime.Now.ToLongDateString();
                        }
                        if (Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()) > Convert.ToDateTime(times[1] + ":00"))
                        {
                            return_val = DateTime.Now.AddDays(1).ToLongDateString();
                        }
                        break;
                    }
                    else
                    {
                        DateTime time = new DateTime();
                        time = DateTime.Now.AddDays(1);
                        if ((int)time.DayOfWeek <= Convert.ToInt32(strarr[k]))
                        {
                            week = Convert.ToInt32(strarr[k]) - (int)time.DayOfWeek;
                            return_val = DateTime.Now.AddDays(1 + week).ToLongDateString();
                            break;
                        }
                    }
                }
               
            }


            return_val = return_val + times[0] + "时00分";
        }
        return return_val;
    }

    /// <summary>
    /// 得到游戏是否关闭
    /// </summary>
    /// <returns></returns>
    public bool GetState()
    {

        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "GetState");

        if (dat != null && dat.Rows.Count > 0)
        {
            if (Convert.ToInt32(dat.Rows[0][0]) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }

    }

    /// <summary>
    /// 修改游戏状态
    /// </summary>
    public void UpdateState()
    {
        SqlHelper.ExecuteNonQuery(constr, CommandType.StoredProcedure, "UpdateState");
    }

    /// <summary>
    /// 判断游戏是否开始
    /// </summary>
    /// <returns></returns>
    public bool IsStart()
    {
        DateTime time = DateTime.Now;
        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "IsStart");
        bool bo = false;
        if (dat != null && dat.Rows.Count > 0)
        {
            string[] strTime = dat.Rows[0][2].ToString().Split(',');

            if (Convert.ToDateTime(time.ToShortTimeString()) >= Convert.ToDateTime(strTime[0] + ":00") && Convert.ToDateTime(time.ToShortTimeString()) <= Convert.ToDateTime(strTime[1] + ":00"))
            {
                switch (Convert.ToInt32(dat.Rows[0][0]))
                {
                    case 1:
                        string[] strDate = dat.Rows[0][1].ToString().Split(',');
                        if (Convert.ToDateTime(time.ToShortDateString()) >= Convert.ToDateTime(strDate[0]) && Convert.ToDateTime(time.ToShortDateString()) <= Convert.ToDateTime(strDate[1]))
                        {
                            bo = true;
                        }

                        break;
                    case 2:
                        if (dat.Rows[0][1].ToString().IndexOf(Convert.ToInt32(time.DayOfWeek).ToString()) >= 0)
                        {
                            bo = true;
                        }

                        break;
                }
            }
        }
        return bo;
    }

    /// <summary>
    /// 获取奖励设置数据
    /// </summary>
    /// <param name="idx">用户id</param>
    /// <param name="level">奖励等级</param>
    /// <returns></returns>
    public int[] GetReward(int idx, int level)
    {

        int[] reward = new int[2];
        DataTable dat = new DataTable();
        SqlParameter[] pars ={ SqlHelper.MakeInParam("@idx", SqlDbType.Int, 4, idx), SqlHelper.MakeInParam("@level", SqlDbType.Int, 4, level) };
        dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "GetReward", pars);

        if (dat != null && dat.Rows.Count > 0)
        {
            reward[0] = Convert.ToInt32(dat.Rows[0][0]);
            reward[1] = Convert.ToInt32(dat.Rows[0][1]);
            return reward;
        }
        else
        {
            return null;
        }

    }

    /// <summary>
    /// 得到前三名奖励
    /// </summary>
    /// <param name="rank">排名</param>
    /// <returns></returns>
    public int[] GetTop(int rank)
    {
        SqlParameter[] pars ={ SqlHelper.MakeInParam("@rank", SqlDbType.Int, 4, rank) };
        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "GetTop", pars);
        int[] top = new int[2];
        top[0] = Convert.ToInt32(dat.Rows[0][0]);
        top[1] = Convert.ToInt32(dat.Rows[0][1]);
        return top;
    }

    /// <summary>
    /// 获取路口设置
    /// </summary>
    /// <returns></returns>
    public string GetWay(int level)
    {
        SqlParameter[] pars ={ SqlHelper.MakeInParam("@level", SqlDbType.Int, 4, level) };
        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "GetWay",pars);
        if (dat.Rows.Count > 0)
        {
           string  way = dat.Rows[0][0].ToString();
            return way;
        }
        else
        {
            return null;
        }

    }

    /// <summary>
    /// 获取游戏设置
    /// </summary>
    /// <returns></returns>
    public string[] GetSet()
    {

        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "GetSet");
        if (dat.Rows.Count > 0)
        {
            string[] result = new string[16];
            for (int k = 0; k < 16; k++)
            {
                result[k] = dat.Rows[0][k].ToString();
            }
            return result;
        }
        else
        {
            return null;
        }


    }

    /// <summary>
    /// 修改活动设置信息
    /// </summary>
    /// <param name="type">模式类型</param>
    /// <param name="date">活动日期</param>
    /// <param name="time">活动时间</param>
    /// <param name="way">出口设置</param>
    /// <param name="FiveExperience">5层经验</param>
    /// <param name="FiveGold">5层金币</param>
    /// <param name="TenExperience">10层经验</param>
    /// <param name="TenGold">10层金币</param>
    /// <param name="FifteenExperience">15层经验</param>
    /// <param name="FifteenGold">15层金币</param>
    /// <returns></returns>
    public bool add_update(int type, string date, string time, string way, string FiveExperience, string FiveGold, string TenExperience, string TenGold, string FifteenExperience, string FifteenGold, string firstExperience, string firstGold, string twoExperience, string twoGold, string threeExperience, string threeGold)
    {
        if (type > 0 && !string.IsNullOrEmpty(date) && !string.IsNullOrEmpty(time) && !string.IsNullOrEmpty(way))
        {
            way = way + ",5";
            if (string.IsNullOrEmpty(FiveExperience))
            {
                FiveExperience = "0";
            }

            if (string.IsNullOrEmpty(FiveGold))
            {
                FiveGold = "0";
            }

            if (string.IsNullOrEmpty(TenExperience))
            {
                TenExperience = "0";
            }

            if (string.IsNullOrEmpty(TenGold))
            {
                TenGold = "0";
            }

            if (string.IsNullOrEmpty(FifteenExperience))
            {
                FifteenExperience = "0";
            }

            if (string.IsNullOrEmpty(FifteenGold))
            {
                FifteenGold = "0";
            }
            if (string.IsNullOrEmpty(firstExperience))
            {
                firstExperience = "0";
            }
            if (string.IsNullOrEmpty(firstGold))
            {
                firstGold = "0";
            }
            if (string.IsNullOrEmpty(twoExperience))
            {
                twoExperience = "0";
            }
            if (string.IsNullOrEmpty(twoGold))
            {
                twoGold = "0";
            }
            if (string.IsNullOrEmpty(threeExperience))
            {
                threeExperience = "0";
            }
            if (string.IsNullOrEmpty(threeGold))
            {
                threeGold = "0";
            }
            SqlParameter[] sqlpars ={ 
            SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
            SqlHelper.MakeInParam("@date",SqlDbType.VarChar,25,date),
            SqlHelper.MakeInParam("@time",SqlDbType.VarChar,25,time),
            SqlHelper.MakeInParam("@way",SqlDbType.VarChar,10,way),
            SqlHelper.MakeInParam("@FiveExperience",SqlDbType.Int,4,Convert.ToInt32(FiveExperience)),
            SqlHelper.MakeInParam("@FiveGold",SqlDbType.Int,4,Convert.ToInt32(FiveGold)),
            SqlHelper.MakeInParam("@TenExperience",SqlDbType.Int,4,Convert.ToInt32(TenExperience)),
            SqlHelper.MakeInParam("@TenGold",SqlDbType.Int,4,Convert.ToInt32(TenGold)),
            SqlHelper.MakeInParam("@FifteenExperience",SqlDbType.Int,4,Convert.ToInt32(FifteenExperience)),
            SqlHelper.MakeInParam("@FifteenGold",SqlDbType.Int,4,Convert.ToInt32(FifteenGold)),
                SqlHelper.MakeInParam("@firstExperience",SqlDbType.Int,4,Convert.ToInt32(firstExperience)),
                    SqlHelper.MakeInParam("@firstGold",SqlDbType.Int,4,Convert.ToInt32(firstGold)),
                    SqlHelper.MakeInParam("@twoExperience",SqlDbType.Int,4,Convert.ToInt32(twoExperience)),
                    SqlHelper.MakeInParam("@twoGold",SqlDbType.Int,4,Convert.ToInt32(twoGold)),
                    SqlHelper.MakeInParam("@threeExperience",SqlDbType.Int,4,Convert.ToInt32(threeExperience)),
                    SqlHelper.MakeInParam("@threeGold",SqlDbType.Int,4,Convert.ToInt32(threeGold))
            };

            int len = SqlHelper.ExecuteNonQuery(constr, CommandType.StoredProcedure, "add_update", sqlpars);
            if (len > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        else
        {
            return false;
        }
    }


}
